const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: '',
    orderIndex: 0, // 商品
    orderState: '',
    order_list: [],
    type: 'ALL',
    keywords: '',
    order_type: [],
    page: 0
  },
  //分类切换
  orderTap: function (e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      page: 0,
      order_list: []
    })
    this.getInspectorOrderList();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
        is_login_inspector = wx.getStorageSync('is_login_inspector');
      if (!is_login_inspector || is_login_inspector != 1) {
        wx.showToast({
          title: '请先登陆验房师账号',
          icon: 'none',
          duration: 1500
        })
        wx.switchTab({
          url: '/pages/my/my',
        })
        return false;
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
   onShow: async function () {
    this.setData({
      order_list: [],
      page: 0,
    })
    this.getUserInfo();
    await this.getInspectorOrderType();
    this.getInspectorOrderList();
  },
  /**
   * @xk 获取用户信息
   */
  getUserInfo: function () {
    var that = this,
        login_status = wx.getStorageSync('login_status'),
        user_id = wx.getStorageSync('user_id');
    if (user_id > 0 && login_status) {
      https.post('getUserInfo', {
        user_id: user_id
      }, false, '加载中').then(res => {
        that.setData({
          user_info: res
        })
      })
    }
  },
  /**
   * @xk 获取验房师订单类型
   */
  getInspectorOrderType: function(){
    var that = this,
        inspector_id = wx.getStorageSync('inspector_id');
    return https.post('getInspectorOrderType',{},true).then(res=>{
      that.setData({
        order_type: res,
        type: res[0].id,
        inspector_id
      })
    })
  },
  /**
   * @xk 获取验房师订单
   */
  getInspectorOrderList: function(){
    var that = this,
        inspector_id = wx.getStorageSync('inspector_id'),
        type = this.data.type,
        page = this.data.page,
        order_list = this.data.order_list,
        keywords = this.data.keywords;
    https.post('getInspectorOrderList',{inspector_id: inspector_id, type: type, page: page, keywords: keywords,is_page: 1},false,'加载中').then(res=>{
      if(res.list){
        order_list.push.apply(order_list,res.list);
      }
      that.setData({
        order_list: order_list,
        page: res.page
      })
    })
  },
  chanagekeywords: function(obj){
    this.setData({
      keywords: obj.detail.value
    })
  },
  search: function(obj){
    var keywords = this.data.keywords;
    if(keywords != ''){
      this.setData({
        page: 0,
        order_list: []
      })
      this.getInspectorOrderList();
    }
  },
  chanageNum: function(obj){
    //切换账号
    wx.removeStorageSync('is_login_inspector');
    wx.removeStorageSync('inspector_id');
    wx.navigateBack({
      delta: 0,
    })
  },
  // 切换备注模式
  markOrder(event) {
    console.log(event.target.dataset.orderid)
    const orderid = event.target.dataset.orderid
    const order_list = JSON.parse(JSON.stringify(this.data.order_list))
    const index = order_list.findIndex(item =>item.id === orderid )
    if (index > -1) {
      order_list[index].marking = true // 改为正在编辑
      this.setData({
        order_list: order_list
      })
    }
  },
  // 提交备注
  confirmRemark(event) {
    console.log(event)
    const newVal = event.detail.value; // 新的值
    const {orderid, rawVal} = event.target.dataset // 订单id, 原值
    const order_list = JSON.parse(JSON.stringify(this.data.order_list))
    const index = order_list.findIndex(item =>item.id === orderid )
    if (newVal === rawVal) {
      return
    }
    if (index > -1) {
      order_list[index].marking = false // 取消正在编辑
      order_list[index].remark = newVal
      this.setData({
        order_list: order_list
      })
    }
    var inspector_id = wx.getStorageSync('inspector_id');
    https.post('updateInspectorOrderRemark',{inspector_id, id: orderid, remark: newVal},false,'提交中').then(res=>{
      console.log('提交成功')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      order_list: []
    })
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getInspectorOrderList();
  },
})
